package cn.kgc.tangcco.io.inputstreamandoutputstram;

import org.junit.Test;

import java.io.*;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/9/9 下午2:03
 */
public class InputStreamAndOutPutstreamTest01 {
    @Test
    public void test01() {
        // 声明文件位置
        File file = new File("src/main/resources/a.txt");
        // 声明字节输入、流
        InputStream inputStream = null;
        try {
            // 实例化FileInpuutSDtream
            inputStream = new FileInputStream(file);
            // 使用read方法读取文件内容
            int read = inputStream.read();
            System.out.println(read);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test02() {
        // 声明字节输入、流
        InputStream inputStream = null;
        try {
            // 实例化FileInpuutSDtream
            inputStream = new FileInputStream("src/main/resources/a.txt");
            // 使用read方法读取文件内容
            int read = inputStream.read();
            while (read != -1) {
                System.out.print((char) read);
                // 继续读取
                read = inputStream.read();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test03() {
        // 声明字节输入、流
        InputStream inputStream = null;
        try {
            // 实例化FileInpuutSDtream
            inputStream = new FileInputStream("src/main/resources/a.txt");
            // 使用read方法读取文件内容
            int read = -1;
            while ((read = inputStream.read()) != -1) {
                System.out.print((char) read);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Test
    public void test04() {
        // 声明字节输入、流
        InputStream inputStream = null;
        try {
            // 实例化FileInpuutSDtream
            inputStream = new FileInputStream("src/main/resources/a.txt");
            byte[] b = new byte[1024];
            // 使用read方法读取返回的是字符串占用字节
            int read = inputStream.read(b);
            System.out.println(read);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test05() {
        // 声明字节输入、流
        InputStream inputStream = null;
        try {
            // 实例化FileInpuutSDtream
            // inputStream = new FileInputStream("src/main/resources/a.txt");
            inputStream = new FileInputStream("pom.xml");
            byte[] b = new byte[1024 * 8];
            // 使用read方法读取返回的是字符串占用字节
            int read = -1;
            while ((read = inputStream.read(b)) != -1) {
                System.out.println(new String(b, 0, read));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
